<template>
  <div>
    <vxe-toolbar
      id="shipmentCompanyListId"
      :setting="{ storage: true }"
      export
      :refresh="{ query: findList }"
    >
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button   v-if="branch.level == 0"  @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      :show-footer="true"
      highlight-hover-row
      row-id="id"
      height="700"
      align="center"
       :loading="loading"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column
          field="authStatus"
          title="审核状态"
          :formatter="formatterAuthStatus"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select
            v-model="listQuery.authStatus"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in authStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="companyName"
          title="公司名称"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input
            v-model="listQuery.companyName"
            placeholder="请输入公司名称"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="phone"
          title="手机号"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input
            v-model="listQuery.phone"
            placeholder="请输入手机号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="serialNumber"
          title="入驻商编码"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input
            v-model="listQuery.serialNumber"
            placeholder="请输入入驻商编码"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="freezingAmount"
          title="冻结金额(元)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.freezingAmountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.freezingAmountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="taxPoint"
          title="税点(%)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.taxPointStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.taxPointEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="isIndependentCheckout"
          title="是否独立核算"
          :formatter="formatterCommonStatus"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select
            v-model="listQuery.isIndependentCheckout"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in commonStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="amount"
          title="账户金额(元)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input
                v-model="listQuery.amountStart"
                placeholder="下限"
                clearable
              />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input
                v-model="listQuery.amountEnd"
                placeholder="上限"
                clearable
              />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="创建时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="authTime"
          title="审核时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtAuthRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'authTime')"
          />
        </template>
      </vxe-table-column>

      <vxe-table-column title="操作" min-width="100" fixed="right">
        <template slot-scope="scope">
          <el-tooltip   v-if="branch.level == 0"  class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <!-- <el-tooltip   v-if="branch.level == 0"  class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip> -->
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :page-sizes="Pagingconf"
      :layouts="[
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'Sizes',
        'FullJump',
        'Total'
      ]"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import { getShipperList, delShipperInfo } from "@/api/shipment";
export default {
  name: "ShipmentCompanyList",
  data() {
    return {
      branch: this.$store.getters.branch,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      gmtAuthRange: "",
      listQuery: {
        // authStatus: "",
        // userName: "",
        // phone: "",
        // freezingAmount: "",
        // amount: "",
        // taxPoint: "",
        // isIndependentCheckout: "",
        // gmtCreatedStart: "",
        // gmtCreatedEnd: "",
        // authTimeStart: "",
        // authTimeEnd: "",
        staffType: "1", // 类型(企业)
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        total: 0
      },
      Pagingconf: [],
      sumObject: {
        freezingAmount: 0,
        amount: 0
      },
      // 审核状态
      authStatusList: [
        { label: "未审核", value: 0 },
        { label: "审核中", value: 1 },
        { label: "审核未通过", value: 2 },
        { label: "审核通过", value: 3 }
      ],
      // 通用是否状态
      commonStatusList: [
        { label: "否", value: 0 },
        { label: "是", value: 1 }
      ]
    };
  },
  created() {
    this.findList();
  },
  methods: {
    findList() {
      return new Promise(resolve => {
        this.loading = true;
        getShipperList(this.listQuery).then(response => {
          resolve();
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.Pagingconf = [20,30,50,100,response.data.total];
          this.sumObject = response.data.sumObject;
          this.loading = false;
        });
      })
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      this.listQuery.sortField = property;
      this.listQuery.sortType = order;
      this.findList();
    },
    // 添加方法
    funcAdd() {
      this.$router.push({
        path: "/basicInfo/shipment/addShipmentCompanyForm"
      });
    },
    // 修改方法
    funcEdit(row) {
      this.$router.push({
        path: "/basicInfo/shipment/editShipmentCompanyForm",
        query: { code: row.code, partyCode: row.partyCode }
      });
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delShipperInfo({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 翻译审核状态
    formatterAuthStatus({ cellValue }) {
      const item = this.authStatusList.find(item => item.value === cellValue);
      return item ? item.label : "";
    },
    // 翻译通用是否状态
    formatterCommonStatus({ cellValue }) {
      const item = this.commonStatusList.find(item => item.value === cellValue);
      return item ? item.label : "";
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 底部颜色设置
    /*    footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return 'col-blue'
        } else {
          return 'col-red'
        }
      }
    }, */
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (columnIndex === 3) {
            // 冻结金额
            return this.sumObject.freezingAmount;
          }
          if (columnIndex === 4) {
            // 账户金额
            return this.sumObject.amount;
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>
